#include "stdafx.h"

TEST(db, with_result)
{
	MYSQL_RES res;
	DBM->query("SELECT * FROM t_user", &res);
	if(NULL == &res)
	{
		LOG_ERR("Null result");
		return;
	}
	LOG_DEBUG(integer(mysql_num_rows(&res)));
}

TEST(db, insert_data)
{
	DBM->query("DELETE FROM t_user WHERE account LIKE 'jackong%'");

	string qry;
	string qry_start = "INSERT INTO t_user(account, password) values('jackong";
	string qry_end = "', '123456')";
	DBM->prepare_manual_commit();
	for (size_t i = 0; i < 500; ++i)
	{
		qry.append(qry_start);
		qry.append(boost::lexical_cast<string>(i));
		qry.append(qry_end);
		DBM->query_non_lock(qry.c_str());
		qry.clear();
	}
	DBM->commit();
}

TEST(db, escape)
{
	string qry = "INSERT INTO t_user(account, password) values('jackong', '";
	string pwd = "1'2*3\4%56";
	DBM->escape(pwd);
	qry.append(pwd);
	qry.append("')");
	ASSERT_TRUE(DBM->query(qry.c_str()));
	DBM->query("DELETE FROM t_user WHERE account = 'jackong'");
}

TEST(db, commit_all_type)
{
	DBM->prepare_manual_commit();
	EXPECT_TRUE(DBM->query_non_lock("INSERT INTO t_user(account, password) values('jackong', '123456')"));
	EXPECT_TRUE(DBM->query_non_lock("UPDATE t_user SET password = '654321' WHERE account = 'jackong'"));
	EXPECT_TRUE(DBM->query_non_lock("DELETE FROM t_user WHERE account = 'jackong'"));
	EXPECT_TRUE(DBM->query_non_lock("DELETE FROM t_user WHERE account = 'jackong'"));
	EXPECT_TRUE(DBM->query_non_lock("UPDATE t_user SET password = '654321' WHERE account = 'jackong'"));
	EXPECT_TRUE(DBM->query_non_lock("INSERT INTO t_user(account, password) values('jackong', '123456')"));
	EXPECT_TRUE(DBM->query_non_lock("DELETE FROM t_user WHERE account = 'jackong'"));
	EXPECT_TRUE(DBM->commit());
}

TEST(db, delete_test_data)
{
	DBM->query("DELETE FROM t_user WHERE account LIKE 'jack%'");
}

TEST(db, multi_insert)
{
	size_t count = 200;
	cout<<"Count for inserting:";
	cin>>count;
	LOG_DEBUG("inserting");
	for (size_t i = 0; i < count; ++i)
	{
		DBM->query("INSERT INTO t_user(account, password) values('jackong', '123456')");
	}
	LOG_DEBUG("deleting");
	DBM->query("DELETE FROM t_user WHERE account = 'jackong'");
	
}